package vsvm.gui.forms;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import vsvm.data.source.DelimitedFileDataSource;

public class ConfigureDelimitedDataSourceDialog extends JDialog {

	private JPanel jContentPane = null;
	private JPanel pnlButtons = null;
	private JPanel pnlCenter = null;
	private JLabel lblName = null;
	private JLabel lblFile = null;
	private JLabel lblDelim = null;
	private JTextField txtName = null;
	private JTextField txtDelim = null;
	private JTextField txtFile = null;
	private JButton btnBrowse = null;
	private JButton btnOk = null;
	private JButton btnCancel = null;

	private DelimitedFileDataSource ds = null;
	private ConfigureDelimitedDataSourceDialog me = null;
	
	/**
	 * This method initializes pnlButtons	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlButtons() {
		if (pnlButtons == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(25);
			pnlButtons = new JPanel();
			pnlButtons.setLayout(flowLayout);
			pnlButtons.setPreferredSize(new java.awt.Dimension(10,40));
			pnlButtons.add(getBtnOk(), null);
			pnlButtons.add(getBtnCancel(), null);
		}
		return pnlButtons;
	}

	/**
	 * This method initializes pnlCenter	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlCenter() {
		if (pnlCenter == null) {
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 2;
			gridBagConstraints5.insets = new java.awt.Insets(10,0,5,10);
			gridBagConstraints5.anchor = GridBagConstraints.EAST;
			gridBagConstraints5.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints5.gridy = 1;
			GridBagConstraints gridBagConstraints41 = new GridBagConstraints();
			gridBagConstraints41.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints41.gridy = 1;
			gridBagConstraints41.weightx = 1.0;
			gridBagConstraints41.insets = new java.awt.Insets(10,10,5,0);
			gridBagConstraints41.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints41.gridx = 1;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.gridy = 2;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.insets = new java.awt.Insets(10,10,5,0);
			gridBagConstraints4.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints4.gridx = 1;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridy = 0;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.insets = new java.awt.Insets(10,10,5,0);
			gridBagConstraints3.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints3.gridx = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.insets = new java.awt.Insets(10,15,5,10);
			gridBagConstraints2.gridy = 2;
			lblDelim = new JLabel();
			lblDelim.setText("Delimiters");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints1.insets = new java.awt.Insets(10,15,5,10);
			gridBagConstraints1.gridy = 1;
			lblFile = new JLabel();
			lblFile.setText("File");
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new java.awt.Insets(10,15,5,10);
			gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints.gridy = 0;
			lblName = new JLabel();
			lblName.setText("Name");
			pnlCenter = new JPanel();
			pnlCenter.setLayout(new GridBagLayout());
			pnlCenter.add(lblName, gridBagConstraints);
			pnlCenter.add(lblFile, gridBagConstraints1);
			pnlCenter.add(lblDelim, gridBagConstraints2);
			pnlCenter.add(getTxtName(), gridBagConstraints3);
			pnlCenter.add(getTxtDelim(), gridBagConstraints4);
			pnlCenter.add(getTxtFile(), gridBagConstraints41);
			pnlCenter.add(getBtnBrowse(), gridBagConstraints5);
		}
		return pnlCenter;
	}

	/**
	 * This method initializes txtName	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtName() {
		if (txtName == null) {
			txtName = new JTextField();
		}
		return txtName;
	}

	/**
	 * This method initializes txtDelim	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtDelim() {
		if (txtDelim == null) {
			txtDelim = new JTextField();
		}
		return txtDelim;
	}

	/**
	 * This method initializes txtFile	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtFile() {
		if (txtFile == null) {
			txtFile = new JTextField();
		}
		return txtFile;
	}

	/**
	 * This method initializes btnBrowse	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnBrowse() {
		if (btnBrowse == null) {
			btnBrowse = new JButton();
			btnBrowse.setPreferredSize(new java.awt.Dimension(18,18));
			btnBrowse.setText("...");
			btnBrowse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
					if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(ConfigureDelimitedDataSourceDialog.this)) {
						txtFile.setText(fc.getSelectedFile().getPath());
					}
				}
			});
		}
		return btnBrowse;
	}

	/**
	 * This method initializes btnOk	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnOk() {
		if (btnOk == null) {
			btnOk = new JButton();
			btnOk.setPreferredSize(new java.awt.Dimension(80,25));
			btnOk.setText("OK");
			btnOk.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (txtName.getText().trim().equals("")) {
						JOptionPane.showMessageDialog(me, "Name cannot be empty!", "Error", 0);
						return;
					}					
					if (txtDelim.getText().equals("")) {
						JOptionPane.showMessageDialog(me, "Delimiters cannot be empty!", "Error", 0);
						return;
					}
					if (txtFile.getText().trim().equals("")) {
						JOptionPane.showMessageDialog(me, "File cannot be empty!", "Error", 0);
						return;
					}
					
					me.ds.setName(txtName.getText());
					me.ds.setDelimiter(txtDelim.getText());
					me.ds.setFilePath(txtFile.getText());
					
					me.dispose();
				}
			});
		}
		return btnOk;
	}

	/**
	 * This method initializes btnCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setPreferredSize(new java.awt.Dimension(80,25));
			btnCancel.setText("Cancel");
			btnCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					me.dispose();
				}
			});
		}
		return btnCancel;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	/**
	 * This is the default constructor
	 */
	public ConfigureDelimitedDataSourceDialog(Frame parent, DelimitedFileDataSource ds) {
		super(parent);
		this.ds = ds;
		this.me = this;
		initialize();
		
		setLocationRelativeTo(parent);
		
		loadData();
	}

	private void loadData() {
		txtName.setText(ds.getName());
		txtDelim.setText(ds.getDelimiter());
		txtFile.setText(ds.getFilePath());		
	}
	
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(402, 207);
		this.setTitle("Configure delimited file data source");
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getPnlButtons(), java.awt.BorderLayout.SOUTH);
			jContentPane.add(getPnlCenter(), BorderLayout.NORTH);
		}
		return jContentPane;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
